module LogiLang.Instantiate (
	instantiate,
	instantiateQ
	) where

import LogiLang.Environment
import LogiLang.Query


instantiate :: Pattern -> Environment -> Pattern
instantiate pat@(Singleton _) _ = pat
instantiate (Combination children) env =
	Combination $ map (\pat -> instantiate pat env) children 
instantiate (Variable var) env =
	case lookupId env var of
		Nothing -> error ("Unbound variable: " ++ var)
		Just value ->
			instantiate value env

instantiate pat _ =
	error ("Cannot instantiate: " ++ show pat)       

instantiateQ :: Query -> Environment -> Query
instantiateQ (SimpleQuery pat) env =
	SimpleQuery (instantiate pat env)
instantiateQ (CompoundQuery connective subqueries) env =
	CompoundQuery connective (map (\q -> instantiateQ q env) subqueries)
